package com.ruiyian.web.controller.system;

import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.ruiyian.common.core.redis.RedisCache;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
public class Eventcontroller extends BaseController {
    @Autowired
    private RedisCache redisCache;


    @PostMapping("/file")
    @ResponseBody
    public String uploadFile(MultipartFile video, MultipartFile picture, HttpServletRequest request) throws IOException {
        // MultipartFile video, MultipartFile picture 这两个参数和python files字典中的两个文件对应
        if (video.isEmpty()) {
            System.out.println("空文件");
            return "emptyfile";
        }
        else{
            String fileName = video.getOriginalFilename(); // 传入的文件名
            String file2name = picture.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + "\\userfile\\" + fileName;  // 生成本地文件地址
            String file2Path = System.getProperty("user.dir") + "\\userfile\\" + file2name;  // 生成本地文件地址
            System.out.println(filePath);
            File dest = new File(filePath);  //创建文件
            File dest2 = new File(file2Path);  //创建文件
            video.transferTo(dest);
            picture.transferTo(dest2);
            return "succeed";
        }
    }

    @PostMapping("/event")
    public  String eventBroadcast(@RequestBody String param){
        int start = param.indexOf("@") + 1;
        int end = param.indexOf("/h264");
        String ipaddress = param.substring(start, end); // ip地址
        System.out.println(start);
        System.out.println(end);
        char flag = param.charAt(param.length() - 1);
        System.out.println(ipaddress);

        HashMap<String, Integer> neweventhashMap = new HashMap<>();
        neweventhashMap = redisCache.getCacheObject("eventdic");
        if(flag == '1'){
            neweventhashMap.put(ipaddress, 1);
            neweventhashMap.put("eventflag", 1);
        }
        else {
            neweventhashMap.put(ipaddress, 0);
            neweventhashMap.put("eventflag", 0);
        }
        redisCache.setCacheObject("eventdic", neweventhashMap);
        neweventhashMap.clear();
        neweventhashMap = redisCache.getCacheObject("eventdic");
        System.out.println(neweventhashMap.toString());
        return "succeed";
    }

    @GetMapping("/screen")
    public HashMap<String, Integer> screenGetinfo(){
        HashMap<String, Integer> neweventhashMap = new HashMap<>();
        neweventhashMap = redisCache.getCacheObject("eventdic");

        HashMap<String, Integer> cleareventhashMap = new HashMap<>();
        cleareventhashMap.put("eventflag", 0);
        redisCache.setCacheObject("eventdic", cleareventhashMap);

        return neweventhashMap;
    }
}
